package com.itheima.sh.controller;

import com.itheima.sh.clients.UserFeign;
import com.itheima.sh.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("feign")
public class FeignController {

    @Autowired
    UserFeign userFeign;

    @GetMapping("{id}")
    //@HystrixCommand(fallbackMethod="queryByIdFallBack")
    public User queryById(@PathVariable("id") String id){

        if (id.equals("itheima")) {
            throw new RuntimeException("itheima");
        }

        User user = userFeign.queryById(id);
        return user;
    }

    /**
     * 降级方法
     * @param
     * @return
     */
//    public User queryByIdFallBack(String id) {
//        User user = new User();
//        user.setName("-1");
//        user.setUsername(id);
//        user.setNickName("暂停服务");
//        return user;
//    }


    @GetMapping("/list")
    public User list(@RequestParam("username") String username, @RequestParam("name") String name) {
        User user = userFeign.list(username, name);
        System.out.println(user);

        return user;
    }

}
